package com.theory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.theory.pojo.Question;
import com.theory.pojo.R;
import com.theory.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/question")
public class QuestionController implements ApplicationRunner {

    @Resource
    private QuestionService questionService;

    @GetMapping("/listPage/{page}/{limit}")
    public R listPage(@PathVariable Long page, @PathVariable Long limit) {
        Page<Question> pageParam = new Page<>(page, limit);
        IPage<Question> pageModel =  questionService.listPage(pageParam);
        return R.ok().data("pageModel", pageModel);
    }


    /**
     * 自动解析json数据，并且导入数据库
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        questionService.remove(null);
        String fileName = "giao.json";
//        里面有调用OCR接口的方法，测试的时候请更改配置文件LIMIT属性以限制OCR接口调用次数
        questionService.jsonToBean(fileName);
    }
}
